package com.weixx.controller;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * restful风格
 * @Auther weixx
 * @Date 2022/1/10
 */
@Controller
@RequestMapping("/Res")
public class RestFulController {
    //原来的 ： http://localhost:8080/add?a=1&b=2
    //Restful ： http://localhost:8080/add/a/b

    @RequestMapping(name="/add/{a}/{b}", method= RequestMethod.GET)
    //@GetMapping("/add/{a}/{b}")
    public String test(@PathVariable  int a,@PathVariable int b, Model model){
        int res=a+b;
        model.addAttribute("msg","结果1为："+res);
        return "hello";
    }

    @RequestMapping(name="/add/{a}/{b}",
            method= RequestMethod.POST)
    //@GetMapping("/add/{a}/{b}")
    public String test2(@PathVariable  int a, @PathVariable int b, Model model, HttpRequest req){
        int res=a+b;

        model.addAttribute("msg","结果2为："+res);
        return "redirect:/hello";
    }
}
